package com.qunar.wan.service;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.qunar.wan.common_api.model.Query;
import com.qunar.wan.common_api.model.SubClassify;
import com.qunar.wan.common_api.model.User;
import com.qunar.wan.common_api.model.UserClassify;
import com.qunar.wan.common_api.service.CacheService;
import com.qunar.wan.common_api.service.UserClassifyService;
import com.qunar.wan.common_api.service.UserService;
import com.qunar.wan.common_api.service.mapper.UserClassifyMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * User: meisiqi Date: 13-11-27 Time: 下午3:19
 */
@Service("userClassifyService")
public class UserClassifyServiceImpl extends BaseServiceImpl<UserClassify, UserClassifyMapper> implements
        UserClassifyService {
    @Resource
    private UserService userService;
    @Resource
    private CacheService cacheService;

    @Override
    public List<SubClassify> selectByuserId(int userId) {
        checkArgument(userId > 0, "userId must greater than 0");
        Query query = new Query().fill("userId", userId).fill("alive", 1);
        List<UserClassify> list = selectByQuery(query);
        List<SubClassify> claList = Lists.transform(list, new Function<UserClassify, SubClassify>() {

            public SubClassify apply(UserClassify userClassify) {
                SubClassify subClassify = cacheService.readSingleSubClassifyCache(userClassify.getSubClaId());
                // SubClassify subClassify = subClassifyService.selectSubClassifyById(userClassify.getSubClaId(), true);
                return subClassify;
            }
        });
        return claList;

    }

    @Override
    public List<User> selectUsers(int subClaId) {
        checkArgument(subClaId > 0, "subClaId must greater than 0");
        Query query = new Query().fill("subClaId", subClaId).fill("alive", 1);
        List<UserClassify> list = selectByQuery(query);
        List<User> userList = Lists.transform(list, new Function<UserClassify, User>() {
            @Override
            public User apply(UserClassify userClassify) {
                User user = userService.selectUserById(userClassify.getUserId());
                return user;
            }
        });
        return userList;
    }

    @Override
    public void addUserClassifies(String classifies, int userId) {
        checkArgument(userId > 0, "userId must greater than 0");
        checkNotNull(classifies);
        String[] subClaIds = classifies.split(",");
        for (String subClaId : subClaIds) {
            List<UserClassify> list = selectByQuery(new Query().fill("userId", userId).fill("subClaId", subClaId));
            if (list.size() == 1) {
                UserClassify userClassify = list.get(0);
                if (userClassify.getAlive() == 0) {
                    userClassify.setAlive(1);
                    updateObj(userClassify);
                } else {
                    continue;
                }
            } else {
                UserClassify userClassify = new UserClassify(userId, Integer.parseInt(subClaId));
                insertObj(userClassify);
            }
        }

    }
}
